//TeacherMapper.java
package org.example.demo3.mapper;

import org.apache.ibatis.annotations.*;
import org.example.demo3.model.Course;
import org.example.demo3.model.Teacher;

import java.util.List;

@Mapper
public interface TeacherMapper {
    //查询教师列表
    @Select("select * from teacher")
    List<Teacher> selectAll();

    //删除教师
    @Delete("delete from teacher where id=#{id}")
    int deleteById(int id);

    //添加教师
    @Insert("insert into teacher(name,password,sex,birthday,course_id,professional) values(#{name},#{password},#{sex},#{birthday},#{course.id},#{professional})")
    int insert(Teacher teacher);

    //查询对应id的教师
    @Select("select * from teacher where id=#{id}")
    Teacher selectById(int id);

    //更新教师
    @Update("update teacher set name=#{name},password=#{password},sex=#{sex},birthday=#{birthday},course_id=#{course.id},professional=#{professional} where id=#{id}")
    int update(Teacher teacher);

    //分页查询
    @Select("select * from teacher limit #{formIndex},#{pageSize}")
    @Results({     // column: 数据库字段   property: Teacher类中的属性  javaType: 映射的Java类型
            @Result(column = "course_id",property = "course",javaType = Course.class,
                    one = @One(select = "org.example.demo3.mapper.CourseMapper.selectById"))
    })
    List<Teacher> selectByPage(int formIndex, int pageSize);

    //登录验证
    @Select("select * from teacher where name=#{username} and password=#{password}")
    Teacher selectByNameAndPwd(String username, String password);
}
